package com.esd.db.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.esd.db.dao.workerMapper;
import com.esd.db.model.worker;
import com.esd.db.service.WorkerService;
@Service("WorkerService")
public class WorkerServiceImpl implements WorkerService {
	@Autowired
	private workerMapper workerMapper; 

	public synchronized int deleteByPrimaryKey(Integer workerId) {

		return workerMapper.deleteByPrimaryKey(workerId);
	}

	@Override
	public synchronized int insert(worker record) {

		return workerMapper.insert(record);
	}

	@Override
	public  int insertSelective(worker record) {

		return workerMapper.insertSelective(record);
	}

	@Override
	public synchronized worker selectByPrimaryKey(Integer workerId) {

		return workerMapper.selectByPrimaryKey(workerId);
	}

//	@Override
//	public List<worker> selWorkers() {
//
//		return null;
//	}

	@Override
	public  int updateByPrimaryKeySelective(worker record) {
	
		return workerMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public synchronized int updateByPrimaryKeyWithBLOBs(worker record) {
	
		return workerMapper.updateByPrimaryKeyWithBLOBs(record);
	}

	@Override
	public synchronized int updateByPrimaryKey(worker record) {
	
		return workerMapper.updateByPrimaryKey(record);
	}

	public  int getWorkerIdByUserId(Integer userid) {
		return workerMapper.selectWorkerIdByUserId(userid);
	}

	@Override
	public  worker getWorkerByUserId(Integer userid) {
		return workerMapper.selectWorkerByUserId(userid);
	}

	@Override
	public  worker getWorkerByWorkerPhone(String workerPhone) {
		
		return workerMapper.selectWorkerByWorkerPhone(workerPhone);
	}

	@Override
	public  worker getWorkerByWorkerIdCard(String workerIdCard) {

		return workerMapper.selectWorkerByWorkerIdCard(workerIdCard);
	}

	@Override
	public  worker getWorkerByWorkerDisabilityCard(String workerDisabilityCard) {

		return workerMapper.selectWorkerByWorkerDisabilityCard(workerDisabilityCard);
	}

	@Override
	public  int getCountWorkerIdByUserId(Integer userId) {
		
		return workerMapper.selectCountWorkerIdByUserId(userId);
	}

	@Override
	public  String getWorkerRealNameByWorkerId(int workerId) {
		
		return workerMapper.selectWorkerRealNameByWorkerId(workerId);
	}

	@Override
	public String getDownCOuntByWorkerId(int workerId) {

		return workerMapper.selectDownCountByWorkerId(workerId);
	}

	@Override
	public List<Map<String, Object>> getLikeRealName(String name,int page ,int row, int alipayEnabled){
		Map<String,Object> map = new HashMap<String, Object>();
		if (page == 0) {
			map.put("begin", null);
			map.put("end", null);
		} else {
			map.put("begin", ((page - 1) * row));
			map.put("end", row);
		}
		if(name.trim().length() == 0){
			map.put("userNameCondition", null);
		}else{
			map.put("userNameCondition", name);
		}
		if(alipayEnabled == 9){
			map.put("alipayEnabled", null);
		}else{
			map.put("alipayEnabled", alipayEnabled);
		}
		return workerMapper.selectLikeRealName(map);
	}

	@Override
	public int getCountLikeRealname(String name, int alipayEnabled) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(name.trim().length() == 0){
			map.put("userNameCondition", null);
		}else{
			map.put("userNameCondition", name);
		}
		if(alipayEnabled == 9){
			map.put("alipayEnabled", null);
		}else{
			map.put("alipayEnabled", alipayEnabled);
		}
		return workerMapper.selectCountLikeRealname(map);
	}

	//工作者人数
	public int getWorkerCount() {
		
		return workerMapper.selectWorkerCount();
	}

	//工作者等级查询
	public List<Map<String, Object>> getWorkerLvl(String userNameCondition, int userLvl, int page,int row) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(userNameCondition.trim().length() == 0){
			map.put("userNameCondition", null);
		}else{
			map.put("userNameCondition", userNameCondition);
		}
		if (page == 0) {
			map.put("begin", null);
			map.put("end", null);
		} else {
			map.put("begin", ((page - 1) * row));
			map.put("end", row);
		}
		if(userLvl == 0){
			map.put("userLvl", null);
		}else{
			map.put("userLvl", userLvl);
		}
		return workerMapper.selectWorkerLvl(map);
	}
	//
	public List<worker> getUIdAndWIdByVersionAndWGC(int version,String wgc) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(version < 0){
			map.put("version", null);
		}else{
			map.put("version", version);
		}
		map.put("wgc", wgc);
		
		return workerMapper.selectUIdAndWIdByVersionAndWGC(map);
	}
	//工作者等级查询个数
	public int getWorkerLvlCount(String userNameCondition, int userLvl) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(userNameCondition.trim().length() == 0){
			map.put("userNameCondition", null);
		}else{
			map.put("userNameCondition", userNameCondition);
		}
		if(userLvl == 0){
			map.put("userLvl", null);
		}else{
			map.put("userLvl", userLvl);
		}
		return workerMapper.selectWorkerLvlCount(map);
	}
	//获得发展的工作者工作号
	@Override
	public List<worker> getWorkerIdByUpdateId(int workerId) {
		
		return workerMapper.selectWorkerIdByUpdateId(workerId);
	}

	@Override
	public int checkDownloadByWorkerId(int workerId) {
		//检查改用户当前的下载状态 1是正在下载
		int downloadStatus = workerMapper.selectDownloadStatusByWorkerId(workerId);
		if(downloadStatus!=1){//不是正在下载的状态就直接返回
			return 1;
		}
		Date now = new Date();
		//比较时间判断下载时间是否超过两个小时
		Date taskDownTime = workerMapper.selectTaskDownTimeByWorkerId(workerId);
		
		Calendar cal = Calendar.getInstance();     
		cal.setTime(taskDownTime);     
		cal.add(java.util.Calendar.HOUR_OF_DAY, 2);   //下载两个小时之后才可以重置
		
		if(cal.getTime().after(now)){//如果下载之后两个小时的时间在现在时间之后  说明现在距离下载还不到两个小时
			return 2;  //如果状态处于正在下载 但是下载时间没有超过两个小时 返回2  提示下载未超时
		}
		return 0;
	}
	
	@Override
	public void resetDownloadByWorkerId(int workerId) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("workerId", workerId);
		workerMapper.updateTaskForResetDownload(map);
//		workerMapper.deleteWorkerRecordForResetDownload(map);
		workerMapper.updateWorkerForResetDownload(map);
	}

	@Override
	public int updateByWgc(worker worker) {
		
		return workerMapper.updateByWgc(worker);
	}

	@Override
	public int changeAlipayStatusByWorkerId(int workerId,int alipayStatus) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("workerId", workerId);
		int newStatus;
		if(alipayStatus==1){
			newStatus = 0;
		}else{
			newStatus = 1;
		}
		map.put("newStatus", newStatus);
		return workerMapper.changeAlipayStatusByWorkerId(map);
	}

	@Override
	public int insertSelectiveReturnWorkerId(worker worker) {
		workerMapper.insertSelectiveReturnWorkerId(worker);
		return worker.getWorkerId();
	}

	@Override
	public int editRealNameByWorkerId(String newRealName, int workerId) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("realName", newRealName);
		map.put("workerId", workerId);
		return workerMapper.updateRealNameByWorkerId(map);
	}

	@Override
	public List<Map<String, Object>> getAllWorkerIdAndGroupCodeMap() {
		return workerMapper.selectAllWorkerIdAndGroupCodeMap();
	}
}
